package com.lxy.controller;


import com.lxy.dto.Exposer;
import com.lxy.dto.SecKillExecution;
import com.lxy.dto.SecKillResult;
import com.lxy.enums.SecKillStateEnum;
import com.lxy.exception.RepeatException;
import com.lxy.exception.SecKillCloseException;
import com.lxy.pojo.SecKill;
import com.lxy.service.SecKillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/seckill")
public class SecKillController {

    @Autowired
    private SecKillService secKillService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("list", secKillService.getSecKillList());
        return "list";
    }


    @GetMapping("/{secKillId}/detail")
    public String detail(Model model, @PathVariable("secKillId") Long secKillId) {
        if (secKillId == null) {
            return "redirect:/seckill/list";
        }
        SecKill secKill = secKillService.getById(secKillId);
        if (secKill == null) {
            return "redirect:/seckill/list";
        }
        model.addAttribute("seckill", secKill);
        return "detail";
    }


    @PostMapping(value = "/{secKillId}/exposer", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SecKillResult<Exposer> exposer(@PathVariable("secKillId") Long secKillId) {
        SecKillResult<Exposer> result;
        try {
            Exposer exposer = secKillService.exportSecKillUrl(secKillId);
            result = new SecKillResult<Exposer>(true, exposer);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return result = new SecKillResult<Exposer>(false, e.getMessage());
        }

        return result;
    }


    @PostMapping(value = "/{secKillId}/{md5}/execution", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SecKillResult<SecKillExecution> execute(@PathVariable("secKillId") Long secKillId,
                                                   @PathVariable("md5") String md5,
                                                   @CookieValue(value = "killPhone", required = false) Long userPhone) {
        if (userPhone == null) {
            return new SecKillResult<SecKillExecution>(false, "未注册");
        }
        SecKillResult<SecKillExecution> result;
        try {
            SecKillExecution secKillExecution = secKillService.executeSecKill(secKillId, userPhone, md5);
            result = new SecKillResult<SecKillExecution>(true, secKillExecution);
        } catch (RepeatException e) {
            SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStateEnum.REPEAT_KILL);
            return new SecKillResult<SecKillExecution>(true, secKillExecution);
        } catch (SecKillCloseException e) {
            SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStateEnum.END);
            return new SecKillResult<SecKillExecution>(true, secKillExecution);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            SecKillExecution secKillExecution = new SecKillExecution(secKillId, SecKillStateEnum.INNER_ERROR);
            return new SecKillResult<SecKillExecution>(true, secKillExecution);
        }
        return result;
    }


    @GetMapping("/time/now")
    @ResponseBody
    public SecKillResult<Long> time() {
        Date now = new Date();
        return new SecKillResult<Long>(true, now.getTime());
    }
}
